//
// Copyright (c) 2010 Luís Reis
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
using System;
using System.Collections.Generic;
namespace Pine.Core
{
	public class PineType : Value
	{
		public static readonly PineType BooleanType = new PineType("bool", typeof(BooleanValue));
		public static readonly PineType NumberType = new PineType("number", typeof(Number));
		public static readonly PineType StringType = new PineType("string", typeof(StringValue));
		public static readonly PineType ArrayType = new PineType("array", typeof(ValueList));
		public static readonly PineType TypeType = new PineType("type", typeof(PineType));
		public static readonly PineType FunctionType = new PineType("function", typeof(Function));
		
		static PineType() {
			NumberType.staticFields.Add("pi", new Number(Math.PI));
			NumberType.staticFields.Add("e", new Number(Math.E));
			NumberType.Constructor = new Function(null, (Function f, ValueList rl) => { Number.CastToNumber(rl); });
		}
		
		public Type NativeType { get; private set; }
		public Value Constructor { get; set; }
		private string name;
		private IDictionary<string, Value> staticFields = new Dictionary<string, Value>();
		
		public PineType() {
		}
		
		public PineType(string name) {
			this.name = name;
		}
		
		public PineType(string name, Type type) {
			NativeType = type;
			this.name = name;
		}
		
		public override bool IsCallable {
			get {
				return Constructor != null && Constructor.IsCallable;
			}
		}
		
		public override string ToString ()
		{
			return name;
		}
		
		public override Value GetPineType() {
			return TypeType;
		}
		
		public override Value FieldGet (string fieldname)
		{
			if (staticFields.Keys.Contains(fieldname))
				return staticFields[fieldname];
			return base.FieldGet (fieldname);
		}
		
		public override void Invoke (ValueList rightlist)
		{
			if (Constructor == null)
				throw new PineException("Type " + name + " does not have an associated constructor function.");
			Constructor.Invoke(rightlist);
		}
	}
}

